package com.jagochan.module.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jagochan.common.result.PageVO;
import com.jagochan.module.business.mapper.ArticleMapper;
import com.jagochan.module.business.pojo.po.Article;
import com.jagochan.module.business.pojo.po.Topic;
import com.jagochan.module.business.pojo.po.User;
import com.jagochan.module.business.pojo.vo.ArticleVO;
import com.jagochan.module.business.service.ArticleService;
import com.jagochan.module.business.service.TopicService;
import com.jagochan.module.business.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;


@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {

    @Resource
    private UserService userService;

    @Resource
    private TopicService topicService;

    @Override
    public PageVO<ArticleVO> pageArticle(Long topicId, int pageNo) {
        // 1. 分页查询主表
        Page<Article> page = Page.of(pageNo, 10); // 构造分页对象
        page = lambdaQuery().eq(Article::getTopicId, topicId).page(page); // 查询条件
        List<Article> articleList = page.getRecords(); // 主表文章列表

        // 2. 填充子表信息 → User信息和话题信息(topicName)
        // 2.1 获取子表userId集合和topicId集合
        Set<Integer> userIdSet = articleList.stream().map(Article::getUserId).collect(Collectors.toSet()); // userId集合
        Set<Integer> topicIdSet = articleList.stream().map(Article::getTopicId).collect(Collectors.toSet()); // topicId集合
        // 2.2 根据userId集合和topicId集合批量查询用户MAP和话题MAP
        Map<Long, User> idUserMap = userService.mapByIds(userIdSet);
        Map<Long, Topic> idTopicMap = topicService.mapByIds(topicIdSet);
        // 2.3 填充子表信息
        List<ArticleVO> articleVOList = articleList.stream().map(item -> {
            // 转换为VO
            ArticleVO vo = new ArticleVO();
            BeanUtil.copyProperties(item, vo);
            // 当前文章的作者信息
            User user = idUserMap.get(Long.valueOf(item.getUserId()));
            if (Objects.nonNull(user)) { // 当前文章的作者信息存在
                vo.setName(user.getUsername());
                vo.setAvatar(user.getAvatar());
                vo.setUserStatus(user.getStatus());
                // TODO userSupportAction
                vo.setIsFollow(false); // TODO 根据当前用户和作者关系判断
            }
            // 当前文章的话题信息
            Topic topic = idTopicMap.get(Long.valueOf(item.getTopicId()));
            vo.setTopicName(topic == null ? "" : topic.getTitle()); // 设置话题名称
            // 把文章的图片字符串转换为数组
            String images = item.getImages();
            vo.setImages((images == null || "".equals(images)) ? new String[]{} : images.split(","));
            return vo;
        }).toList();

        // 3. 构建返回值
        PageVO<ArticleVO> result = new PageVO<>();
        result.setCurrent(page.getCurrent());
        result.setSize(page.getSize());
        result.setTotal(page.getTotal());
        result.setList(articleVOList);
        result.setPages(page.getPages());
        return result;
    }

    @Override
    public ArticleVO getArticleDetail(Long id) {
        Article article = lambdaQuery().eq(Article::getId, id).one();
        if (article == null) {
            return null;
        }
        // 转换为VO
        ArticleVO vo = new ArticleVO();
        BeanUtil.copyProperties(article, vo);
        // 把文章的图片字符串转换为数组
        String images = article.getImages();
        vo.setImages((images == null || "".equals(images)) ? new String[]{} : images.split(","));

        // 查询用户(作者)信息
        Integer userId = article.getUserId();
        User user = userService.getById(userId);
        if (user != null) {
            vo.setName(user.getUsername());
            vo.setAvatar(user.getAvatar());
            vo.setUserStatus(user.getStatus());
            // TODO userSupportAction
            vo.setIsFollow(false); // TODO 根据当前用户和作者关系判断
        }

        // 查询话题信息
        Integer topicId = article.getTopicId();
        Topic topic = topicService.getById(topicId);
        vo.setTopicName(topic == null ? "" : topic.getTitle());
        return vo;
    }
}
